define(function (require, exports, module) {
	require("./DrawingTools.js");
    //var CAnnotFactory = WebPDF.CAnnotFactory;
    WebPDF.Tools.TOOL_NAME_INKANNOT = "InkAnnot Tool";
    
    
    WebPDF.Tools.InkAnnotToolHandler = function (annotPlugin) {
        var _readerApp = null,
    		_baseAnnotPlugin = annotPlugin,
    		_isProcessing = false,
			_pencil = null,
            _jqDocViewContainer = null,
            _docView = null,
            _cursorClass = null;
			//_bCursorInited = false,
    		//_self = this;
        //var MKA_POPUPWIDTH = 180;
        //var MKA_POPUPHEIGHT = 120;
        //var DEFAULT_OPCITY = 1;
    
        this.onInit = function (readerApp) {
            _readerApp = readerApp;			
        };

        this.onDestroy = function () {
        };        
        
        this.updateCursor = function() {    
            
            if (WebPDF.Environment.ie || WebPDF.Environment.ieAtLeast11)
                _cursorClass = "fwr-pencil-cursor-ie";               
            else
                _cursorClass = "fwr-pencil-cursor";             
             
             _jqDocViewContainer.addClass(_cursorClass);
         }            
        
         
         
        this.onActivate = function () {                  
            _docView = _readerApp.getMainView().getDocView();
            _jqDocViewContainer = $("#" + _docView.getDocViewContainerID());            
            this.updateCursor();           	
        };
        

        this.onDeactivate = function () {            
             _jqDocViewContainer.removeClass(_cursorClass);             
        };
        
        this.isProcessing = function () {
            return true;
        };

        // Calculate status
        /**
        * @description 氓藛陇忙鈥撀ッヂ仿ッモ�路忙藴炉氓锟铰γワ拷炉莽鈥澛�
        * @return {Boolean} 氓锟铰р�篓猫驴鈥澝モ�啪true,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.Tool_IsEnabled = function () {
            var pCurrentDocument = _pReaderApp.getMainView().getDocView().getPDFDoc();
            if (pCurrentDocument) {
                if (pCurrentDocument.getDocType() != WebPDF.PDFData.DocType.XFA)
                    return true;
            }
            /*
            * if (!(pDoc->GetDocumentType() == DOCTYPE_DYNIMIC_XFA))
            return pDoc->GetPermissions(FPDFPERM_ANNOT_FORM) ; 
            * */
            return true;
        };

        /**
        * @description 氓藛陇忙鈥撀ッヂ仿ッモ�路忙藴炉氓锟铰γβＣヂも�盲潞沤忙鈥撯�忙艙卢茅鈧�忙鈥孤┟犅睹︹偓锟�
        * @return {Boolean} 忙颅拢氓陇鈥灻ぢ号矫︹�鈥∶ε撀┾偓鈥懊︹�漏莽艩露忙鈧拷猫驴鈥澝モ�啪true,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.Tool_IsProcessing = function () {
            return _isProcessing;
        };
        
        
        this.getName = function () {
            return WebPDF.Tools.TOOL_NAME_INKANNOT;
        };

        // Mouse events
        /**
        * @description 茅录聽忙聽鈥∶ヂ仿γ┾�庐忙艗鈥懊ぢ糕�盲潞鈥姑ぢ宦睹ヂも�莽锟解�
        * @param {object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onLButtonDown = function (event) {		          
            var docViewRect = _docView.getDocViewClientRect();
            
            if (!WebPDF.RectUtils.ptInRect(docViewRect, event.pageX, event.pageY))
                return true;            
            
            if (!WebPDF.Common.containsNode(_jqDocViewContainer.get(0), event.target, true)) {
                return false;
            }
           
            var visiblePageRange = _docView.getVisiblePageRange();
            var pPageView = null;
            var pageViewRect = null;
            for (var i = visiblePageRange.begin; i <= visiblePageRange.end; i++) {
                var pTmpPageView = _docView.getPageView(i);
                var pageOffsetHtmlDoc = $("#" + pTmpPageView.getPageViewContainerID()).offset();
                var pageViewWidth = pTmpPageView.getPageViewWidth();
                var pageViewHeight = pTmpPageView.getPageViewHeight();
                pageViewRect = new WebPDF.PDFRect(pageOffsetHtmlDoc.left, pageOffsetHtmlDoc.top,
						pageOffsetHtmlDoc.left + pageViewWidth, pageOffsetHtmlDoc.top + pageViewHeight);
                WebPDF.RectUtils.normalize(pageViewRect);
                if (WebPDF.RectUtils.ptInRect(pageViewRect, event.pageX, event.pageY)) {
                    pPageView = pTmpPageView;
                    break;
                }
            }

            if (pPageView != null) {
				WebPDF.Common.preventDefaults(event, true);
                if(_pencil == null)
				{
					_pencil = new WebPDF.DrawingTools.Pencil(_readerApp,
                                                             _baseAnnotPlugin,
                                                             _baseAnnotPlugin.getAnnotHandlerMgr().getUIManager().getCanvasElement(),3, "rgba(255,0,0,1.0)","#ff0000");
				}
				return _pencil.OnMouseDown(event,pPageView);
            }

			return false;
			
			

		
        };

        /**
        * @description 茅录聽忙聽鈥∶ヂ仿γ┾�庐氓录鹿猫碌路盲潞鈥姑ぢ宦睹ヂも�莽锟解�
        * @param {object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onLButtonUp = function (event) {
           // _isProcessing = false;
			if(_pencil != null)
			{	
			   return _pencil.OnMouseUp(event);
			}
		   return false;
        };

        this.createAnnot = function (pPageView, pageXofPageView, pageYofPageView) {
            /*var dt = new Date();
            var time = dt.valueOf() + dt.getTimezoneOffset() * 60000;
            var timestamp = (Math.floor(time / 1000)).toString();
            var annots_json = {
                c: "",
                ca: DEFAULT_OPCITY,
                cd: timestamp,
                cl: "#ffff00",
                f: WebPDF.PDFData.AnnotFlag.ANNOTFLAG_PRINT,
                md: timestamp,
                n: timestamp,
                pop: null,
                subj: "Note",
                subty: "Text",
                tit: _pReaderApp.GetUserName(),
                ty: "Markup",
                Quad: [],
                txtn: 'Comment',

                rc: null
            };

            var pPDFPage = pPageView.GetPDFPage();
            var pdfPageWidth = pPDFPage.GetPageWidth();
            var pdfPageHeight = pPDFPage.GetPageHeight();
            var curPointPDF = new WebPDF.CPDF_Point(pageXofPageView, pageYofPageView);
            curPointPDF = pPageView.DocPt2PDF(curPointPDF);
            var halfWidth = TEXT_ANNOT_DEFAULT_WIDTH / 2;
            var halfHeight = TEXT_ANNOT_DEFAULT_HEIGHT / 2;
            var pdfrect = new WebPDF.CPDF_Rect(curPointPDF.x - halfWidth, curPointPDF.y + halfHeight, curPointPDF.x + halfWidth,
        			 curPointPDF.y - halfHeight);

            if (pdfrect.left < 0) {
                WebPDF.RectUtils.Offset(pdfrect, 0 - pdfrect.left, 0);
            }
            if (pdfrect.bottom < 0) {
                WebPDF.RectUtils.Offset(pdfrect, 0, 0 - pdfrect.bottom);
            }
            if (pdfrect.right > pdfPageWidth) {
                WebPDF.RectUtils.Offset(pdfrect, pdfPageWidth - pdfrect.right, 0);
            }
            if (pdfrect.top > pdfPageHeight) {
                WebPDF.RectUtils.Offset(pdfrect, 0, pdfPageHeight - pdfrect.top);
            }

            annots_json.rc = [pdfrect.left, pdfrect.top, pdfrect.right, pdfrect.bottom];

            var popupRect = new WebPDF.CPDF_Rect(0, 0, 0, 0);
            popupRect.top = pdfrect.top;
            popupRect.right = pdfPageWidth;
            popupRect.left = popupRect.right - MKA_POPUPWIDTH;
            popupRect.bottom = popupRect.top - MKA_POPUPHEIGHT;
			if(popupRect.bottom < 0)
			{
				WebPDF.RectUtils.Offset(popupRect, 0, -popupRect.bottom);
			}

            annots_json.pop =
        	 {
        	     f: WebPDF.PDFData.AnnotFlag.ANNOTFLAG_PRINT,
        	     md: timestamp,
        	     n: timestamp + "_popup",
        	     op: 1,
        	     rc: [popupRect.left, popupRect.top, popupRect.right, popupRect.bottom]
        	 };

            var pAnnot = CAnnotFactory.CreateAnnot(annots_json, false);
            pPDFPage.GetAnnotJsonData().push(annots_json);
            pPDFPage.AddAnnot(pAnnot);
            pPDFPage.SetModifyed(true);
            var pageAnnotsDiv = _pBaseAnnotPlugin.GetHandlerManager().GetPageAnnotsContainerID(pPageView);
            var jqPageAnnotsDiv = $("#" + pageAnnotsDiv);
            var annotName = pAnnot.GetAnnotName();
            var newAnnotHtml = _pBaseAnnotPlugin.GetHandlerManager().CreateAAnnotHtmlContent(pPageView, pAnnot, annotName);
            jqPageAnnotsDiv.append(newAnnotHtml);
			_pBaseAnnotPlugin.GetHandlerManager().SetPopupFocos(pPageView,annotName);*/
            //_pBaseAnnotPlugin.GetHandlerManager().TypewriterAnnot_Edit(pPageView, annotName);
        };



        /**
        * @description 茅录聽忙聽鈥∶ヂ仿γ┾�庐氓锟脚捗モ�禄盲潞鈥姑ぢ宦睹ヂも�莽锟解�
        * @param {object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onLButtonDblClk = function (event) {
            return false;
        };
        /**
        * @description 盲赂禄莽陋鈥斆ぢ解�莽拧鈥灻┞悸犆β犫�莽搂禄氓艩篓盲潞鈥姑ぢ宦睹ヂも�莽锟解�
        * @param {object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onMouseMove = function (event) {
			if(_pencil != null)
				return _pencil.OnMouseMove(event);
            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶ワ拷鲁茅鈥澛ε掆�盲赂鈥姑ぢ衡�盲禄露氓陇鈥灻э拷鈥�
        * @param {object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onRButtonDown = function (event) {
            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶ワ拷鲁茅鈥澛ヂ悸姑德访ぢ衡�盲禄露氓陇鈥灻э拷鈥�
        * @param {object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onRButtonUp = function (event) {
            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶ワ拷鲁茅鈥澛ワ拷艗氓鈥÷幻ぢ衡�盲禄露氓陇鈥灻э拷鈥�
        * @param {object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onRButtonDblClk = function (event) {
            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶β慌∶铰β慌∶ヅ犅ぢ衡�盲禄露氓陇鈥灻э拷鈥�
        * @param {object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onMouseWheel = function (event) {
            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶幻库�盲潞鈥姑ぢ宦睹妓喢┞悸犆β犫�莽搂禄氓艩篓氓藛掳氓庐茠氓颅锟矫モ�茠莽麓聽忙鈥斅睹ぢ古该ぢ寂∶γワ拷鈥樏ッぢ衡�盲禄露茂录鈥懊ヂも�莽锟解�
        * @param {object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onMouseOver = function (event) {
            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶幻モ�潞盲潞鈥姑ぢ宦睹妓喢┞悸犆β犫�莽搂禄氓艩篓氓藛掳氓庐茠氓颅锟矫モ�茠莽麓聽忙鈥斅睹ぢ古该ぢ寂∶γワ拷鈥樏ッぢ衡�盲禄露茂录鈥懊ヂも�莽锟解�
        * @param {object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onMouseOut = function (event) {
            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶库�氓鈥βッぢ衡�盲禄露茂录藛茅录聽忙聽鈥∶ワ拷陋忙艙鈥懊库�氓鈥βッッモ�茠莽麓聽氓艗潞氓鸥鸥忙鈥帮拷盲录拧猫搂娄氓锟解�猫炉楼盲潞鈥姑ぢ宦睹尖�氓陇鈥灻э拷鈥�
        * @param {object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onMouseEnter = function (event) {
            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶β幻ヂ尖偓盲潞鈥姑ぢ宦睹妓喢┞悸犆β犫�氓锟铰ε撯�莽娄禄氓录鈧ッモ�茠莽麓聽氓艗潞氓鸥鸥忙鈥帮拷盲录拧猫搂娄氓锟解�猫炉楼盲潞鈥姑ぢ宦睹尖�氓陇鈥灻э拷鈥�
        * @param {object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onMouseLeave = function (event) {
			if(_pencil != null)
				return _pencil.OnMouseUp(event);
		   return false;
        };
        this.onKeyDown = function (event) {
            return false;
        };
        this.onKeyUp = function (event) {
            return false;
        };
        this.onChar = function (event) {
            return false;
        };
    };

    return WebPDF.Tools.InkAnnotToolHandler;
});